package com.liuyk.commonlib.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Scroller;

public class SlideLayout extends FrameLayout {

    private final Scroller mScroller;
    private final Activity mActivity;
    private final int mTouchSlop;
    private ViewGroup mDecorView;
    private View mSlideView;
    private float mStartTouchX;
    private float mStartTouchY;

    private float mLastX;
    private float mMoveX;
    private float mMoveY;


    private final float mSlideRate = 0.25f;
    private float mAllX;

    private boolean isUp;
    private boolean shouldFinish;

    public SlideLayout(Context context) {
        super(context);
        if (context instanceof Activity) {
            mActivity = (Activity) context;
        } else {
            throw new RuntimeException("context must be a activity");
        }
        mScroller = new Scroller(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mActivity.getWindow().getDecorView().setBackgroundColor(Color.parseColor("#59000000"));
    }


    public void bind() {
        mDecorView = (ViewGroup) mActivity.getWindow().getDecorView();
        mSlideView = mDecorView.getChildAt(0);
        mDecorView.removeView(mSlideView);
        addView(mSlideView);
        setBackgroundColor(Color.TRANSPARENT);
        mDecorView.addView(this);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mStartTouchX = ev.getX();
                mStartTouchY = ev.getY();
                return false;
            case MotionEvent.ACTION_MOVE:
                mLastX = mMoveX = ev.getX();
                mMoveY = ev.getY();
                float dx = mMoveX - mStartTouchX;
                float dy = mMoveY - mStartTouchY;
                // 判断是否右向滑动
                if (Math.abs(dx) > Math.abs(dy) && dx > 0) {
                    return dx > mTouchSlop;
                }
                return false;
            case MotionEvent.ACTION_UP:

                return false;
            default:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mStartTouchX = event.getX();
                mStartTouchY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                mMoveX = event.getX();
                mMoveY = event.getY();
                int dx = (int) (mMoveX - mLastX);
                if ((getScrollX() + (-dx) > 0)) {
                    scrollTo(0, 0);
                } else if ((getScrollX() + (-dx)) < (-mAllX)) {
                    scrollTo((int) -mAllX, 0);
                } else {
                    scrollBy(-dx, 0);
                }
                break;
            case MotionEvent.ACTION_UP:
                isUp = true;
                if (Math.abs(getScrollX()) < mAllX * mSlideRate) {
                    mScroller.startScroll(getScrollX(), 0, -getScrollX(), 0, 300);
                    invalidate();
                } else {
                    shouldFinish = true;
                    mScroller.startScroll(getScrollX(), 0, -(int) (mAllX + getScrollX() - 150), 0, 300);
                    invalidate();
                }
                break;
            default:
                break;

        }
        mLastX = event.getX();
        return true;
    }

    @Override
    public void computeScroll() {
        if (!isUp) {
            return;
        }
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), 0);
            invalidate();
        } else {
            if (shouldFinish) {
                mActivity.finish();
            } else {
                isUp = false;
            }
        }
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mAllX = getWidth();
    }
}
